using System.Security.Claims;
using System.Diagnostics.Contracts;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Mvc;

namespace ShopForWeb.UI.Extensions;

public static class ControllerExtension
{

  public static async Task<string> FileSaveAsync(this Controller controller, IFormFile photofile, string path, string filename)
  {
    if (string.IsNullOrEmpty(filename))
      filename = Guid.NewGuid().ToString("N") + ".jpg";

    FileStream fs = new FileStream(Path.Combine(path, filename), FileMode.Create);

    try
    {
      await photofile.CopyToAsync(fs);
    }
    catch { }
    finally
    {
      fs.Close();
      fs.Dispose();
    }
    return filename;
  }

  public static int GetUserID(this Controller controller)
  {
    return Convert.ToInt32(controller.HttpContext.User.FindFirst(ClaimTypes.Sid)?.Value);
  }


}
